package com.zyao89.view.zloading;

import com.zyao89.view.zloading.util.Log;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.element.Element;
import ohos.agp.render.Canvas;
import ohos.agp.utils.Color;
import ohos.app.Context;

/**
 * Created by zyao89 on 2017/3/19.
 * Contact me at 305161066@qq.com or zyao89@gmail.com
 * For more projects: https://github.com/zyao89
 * My Blog: http://zyao89.me
 */
public class ZLoadingView extends Image implements Component.BindStateChangedListener,
        Element.OnChangeListener, Component.DrawTask {

    private static final String TAG = "Zloading ZLoadingView";
    public boolean isVisiable;
    protected ZLoadingBuilder mZLoadingBuilder;
    private ZLoadingDrawable mZLoadingDrawable;
    private int mPaintcolor;

    public ZLoadingView(Context context) {
        this(context, null);
        this.mContext = context;
    }

    public ZLoadingView(Context context, AttrSet attrs) {
        this(context, attrs, "");
        this.mContext = context;
    }

    public ZLoadingView(Context context, AttrSet attrs, String defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttrSet attrs) {
        try {
            int typeId = attrs.getAttr("z_type").get().getIntegerValue();
            mPaintcolor = attrs.getAttr("z_color").get().getIntegerValue();
            Log.sysI(TAG, "typeId: " + typeId + "mPaintcolor :" + mPaintcolor);
            float durationTimePercent = attrs.getAttr("z_duration_percent").get().getFloatValue();
            setLoadingBuilder(Z_TYPE.values()[typeId], durationTimePercent);
        } catch (Exception e) {
            Log.sysI("MyZloading", e.getMessage());
        }
    }

    public void setLoadingBuilder(Z_TYPE builder) {
        mZLoadingBuilder = builder.newInstance();
        initZLoadingDrawable();
    }

    public void setLoadingBuilder(Z_TYPE builder, double durationPercent) {
        this.setLoadingBuilder(builder);
        initDurationTimePercent(durationPercent);
    }

    private void initZLoadingDrawable() {
        if (mZLoadingBuilder == null) {
            throw new RuntimeException("mZLoadingBuilder is null.");
        }
        mZLoadingDrawable = new ZLoadingDrawable(getContext());
        mZLoadingDrawable.setmZLoadingBuilder(mZLoadingBuilder);
        initParams(getContext(), new Color(mPaintcolor));
        this.addDrawTask(mZLoadingDrawable::onDraw);
        mZLoadingBuilder.setCallback(this);
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        this.mZLoadingBuilder.draw(canvas);
    }

    private void initDurationTimePercent(double durationPercent) {
        if (mZLoadingBuilder == null) {
            throw new RuntimeException("mZLoadingBuilder is null.");
        }
        mZLoadingBuilder.setDurationTimePercent(durationPercent);
    }

    @Override
    public void setVisibility(int visibility) {
        if (getVisibility() != visibility) {
            super.setVisibility(visibility);
            if (visibility == HIDE || visibility == INVISIBLE) {
                startAnimation();
            } else {
                stopAnimation();
            }
        }
    }

    public void startAnimation() {
        if (mZLoadingDrawable != null) {
            mZLoadingDrawable.start();
        }
    }

    public void stopAnimation() {
        if (mZLoadingDrawable != null) {
            mZLoadingDrawable.stop();
        }
    }

    @Override
    public void onComponentBoundToWindow(Component component) {
        Log.sysI(TAG, "onComponentBoundToWindow");
        startAnimation();
    }

    void initParams(Context context, Color color) {
        if (mZLoadingBuilder != null) {
            mZLoadingBuilder.init(context);
            mZLoadingBuilder.initParams(context, color);
        }
    }

    @Override
    public void onComponentUnboundFromWindow(Component component) {
        stopAnimation();
    }

    @Override
    public void onChange(Element element) {
        this.addDrawTask(mZLoadingDrawable::onDraw);
    }
}
